/*************************************************************************
  > File Name: main.c
  > Author: Happyfrog
  > Mail: yanghappyfrog@163.com
  > Created Time: 2017年11月01日 星期三 14时56分01秒
 ************************************************************************/

#include <stdio.h>
#include "libcard.h"

int main(int argc, char* argv[])
{
    pthread_mutex_t mutex;
    rf_card* card;
    sem_t sem;
    short delaytime = 250;
    char cardtype = 0;
    unsigned char UIDLen = 0;
    char cardUID[64];
    int i = 0;
    float money = 0.0;
    char locknum = 0;
    int ret  = 0;

    pthread_mutex_init(&mutex,NULL);
    sem_init(&sem,0,0);

    do{
        card = (rf_card*)malloc(sizeof(rf_card));
    }while(card == NULL);

    strcpy(card->com.name,"/dev/ttyO1");
    card->com.baud = 57600;
    card->com.data_bits = 8;
    card->com.parity = 0;
    card->com.stop_bits = 1;

    protocol_card_init(card,&mutex,&sem);

    /*	protocol_open_rf();

        protocol_enabled_card(delaytime, &cardtype, &UIDLen, cardUID);
        printf("======cardtype:%x UIDLen:%d cardUID: ",cardtype, UIDLen);;
        for(i=0; i<UIDLen; i++)
        printf("%02x ",cardUID[i]);
        printf("\n");

        protocol_close_rf();
     */
    while(1)
    {
        ret = sleep(10);
        while(ret)
            ret = sleep(ret);

        ret = protocol_read_card(&money, &locknum);

        printf("\n############ Card Info ##################\n");
        printf("card.UIDLen = %d\n",card->UIDLen);

        printf("card.cardUID = ");
        for(i=0; i<card->UIDLen; i++)
            printf("%02x ", card->cardUID[i]);
        printf("\n");

        printf("card.cardNUM = ");
        for(i=0; i<8; i++)
            printf("%02x",card->cardNUM[i]);
        printf("\n");
        printf("card.ver = %02x\n",card->ver);
        printf("card.money = %d\n",money);
        printf("card.locknum = %d\n",locknum);
        printf("\n############    Card  ##################\n");

        if(ret == 1){
            printf("\n\n\n\n\n\n\n\n READ CARD ERROR\n\n\n\n\n");
        }


    }
    return 0;
}
